package tmts;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.concurrent.TimeUnit;

import models.Lap;
import models.Skater;

public class SkaterParser
{

    private static final int NO_OF_COLUMS = 3;

    public static Skater parseFile(File skaterFile)
    {
        Skater skater = new Skater();
        try
        {
            @SuppressWarnings("resource")
            BufferedReader reader = new BufferedReader(new FileReader(skaterFile));
            String[] skaterInfo = reader.readLine().split(";");
            checkColumLength(skaterInfo);

            skater.setSkaterId(Integer.parseInt(skaterInfo[0]));
            skater.setSkaterName(skaterInfo[1]);
            skater.setCountry(skaterInfo[2]);

            String[] headers = reader.readLine().split(";");
            checkColumLength(headers);

            if (!headers[0].equals("Distance:"))
                throw new ParseException("Invalid header", 0);
            if (!headers[1].equals("Time:"))
                throw new ParseException("Invalid header", 0);
            if (!headers[2].equals("Lap:"))
                throw new ParseException("Invalid header", 0);

            String line;

            while ((line = reader.readLine()) != null)
            {
                String[] lapValues = line.split(";");
                Lap lap = new Lap();
                lap.setDistance(stringToInt(lapValues[0]));
                lap.setTime(timeToMilliSecconds(lapValues[1]));
                lap.setLaptime(timeToMilliSecconds(lapValues[2]));
                skater.addLap(lap);
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (ParseException e)
        {
            e.printStackTrace();
        }

        return skater;
    }

    private static long timeToMilliSecconds(String time) throws ParseException
    {
        String[] timeValues = time.split(":");

        long timeInMilliSeconds = 0;
        if (timeValues.length == 1)
        {
            timeInMilliSeconds = (long) (Double.parseDouble(timeValues[0]) * 1000);
        } else if (timeValues.length == 2)
        {
            timeInMilliSeconds = TimeUnit.MILLISECONDS.convert(Long.parseLong(timeValues[0]), TimeUnit.MINUTES);
            timeInMilliSeconds += (long) (Double.parseDouble(timeValues[1]) * 1000);
        } else
        {
            throw new ParseException("Time value is to big " + time, 0);
        }

        return timeInMilliSeconds;
    }

    private static int stringToInt(String string)
    {
        char[] chars = string.toCharArray();
        String returnValue = "";

        for (char c : chars)
        {
            if (c >= '0' && c <= '9')
                returnValue += c;
        }

        return Integer.parseInt(returnValue);
    }

    private static void checkColumLength(String[] values) throws ParseException
    {
        if (values.length != NO_OF_COLUMS)
            throw new ParseException("Error on parsing file", 0);
    }
}